package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.IdxSynonymDetailsListVo;
import com.indexingsystem.boss.Vo.IdxSynonymListVo;
import com.indexingsystem.boss.Vo.IdxSynonymReqVo;
import com.indexingsystem.boss.admin.mapper.IdxSynonymDetailsMapper;
import com.indexingsystem.boss.admin.mapper.IdxSynonymMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.IdxSynonym;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IdxSynonymService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IdxSynonymServiceImpl implements IdxSynonymService {

    @Resource
    private IdxSynonymMapper idxSynonymMapper;

    @Resource
    private IdxSynonymDetailsMapper idxSynonymDetailsMapper;

    @Override
    public PagedResult<IdxSynonymListVo> selectIdxSynonyml(IdxSynonymReqVo idxSynonymReqVo) {
        Integer pageNo = idxSynonymReqVo.getPageNo() == null ? 1 : idxSynonymReqVo.getPageNo();
        Integer pageSize = idxSynonymReqVo.getPageSize() == null ? 10 : idxSynonymReqVo.getPageSize();
        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage( pageNo, pageSize );
        return BeanUtils.toPagedResult( idxSynonymMapper.selectIdxSynonyml( idxSynonymReqVo ) );
    }

    @Override
    public Integer selectIdxSynonymlCount(IdxSynonymReqVo idxSynonymReqVo) {
        return idxSynonymMapper.selectIdxSynonymlCount(idxSynonymReqVo);

    }

    @Override
    public Integer insertSelective(IdxSynonym record) {
        return idxSynonymMapper.insertSelective( record );
    }

    @Override
    public Integer updateByPrimaryKeySelective(IdxSynonym record) {
        return idxSynonymMapper.updateByPrimaryKeySelective( record );
    }

    @Override
    public Integer deleteByIdBatch(List<String> ids) {
        List<String> synonymIds = new ArrayList<>();
        List<IdxSynonymDetailsListVo> idxSynonymDetails;
        for (int i=0;i<ids.size();i++) {
            idxSynonymDetails = idxSynonymDetailsMapper.selectIdxSynonymDateislById(ids.get(i));
            if (null != idxSynonymDetails && idxSynonymDetails.size() > 0)
            {
                for (int j=0;j<idxSynonymDetails.size();j++) {
                 synonymIds.add(idxSynonymDetails.get(j).getId());
               }
            }

        }

        if (null != synonymIds && synonymIds.size() > 0) {
            idxSynonymDetailsMapper.deleteByIdBatch(synonymIds);
        }

        return  idxSynonymMapper.deleteByIdBatch( ids );
    }

	@Override
	public Integer updateSynonymStatusByIdBatch(List<IdxSynonym> synonymsList) {
		
		  return idxSynonymMapper.updateSynonymStatusByIdBatch(synonymsList);
	}

    @Override
    public Integer selectCountByIds(List<String> ids) {
        return idxSynonymMapper.selectCountByIds(ids);
    }
}
